package rss;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

/**
 * 
 * RSS Collector - URLdownloader - RSSReader * MAIN PROGRAM
 * 
 * @author Kevin
 * 
 */
public class RSSCollector {

	static String urlRSS;
	static String anchorName;
	static String savedFolderFP;
	static String archivedFolderFP;
	final static String profileFP = "c:\\docdoc\\profile\\RSSCollector.properties";

	/**
	 * Initiate variables from Properties File
	 */
	private static void Init() {
		Properties properties = new Properties();
		try {
			properties.load(new FileInputStream(profileFP));

			urlRSS = properties.getProperty("urlRSS");
			anchorName = properties.getProperty("anchorName");
			savedFolderFP = properties.getProperty("savedFolderFP");
			archivedFolderFP = properties.getProperty("archivedFolderFP");

			properties.list(System.out);
			System.out.println("________________________ \n\n");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		Init();

		URLdownloader downloader = new URLdownloader();
		RSSReader reader = new RSSReader(urlRSS);

		ArrayList<String> list = reader.getURLList();
		Iterator<String> nameIter = list.iterator();

		int count = 0;
		while (nameIter.hasNext()) {

			String urlDownloadPage = (String) nameIter.next();
			String downloadTorrentFileName = getTorrentFileNameFromURL(urlDownloadPage);
			File existTorrent = new File(archivedFolderFP + downloadTorrentFileName);

			System.out.println("URL  " + urlDownloadPage);

			if (existTorrent.exists()) {
				System.out.println("File exists in local. \n");
			} else {
				downloader.download(urlDownloadPage, anchorName, savedFolderFP + downloadTorrentFileName);
				try {
					System.out.println("\n:: Downloaded " + downloadTorrentFileName);
					count++;
					System.out.println("\n( ...Sleep... for 30+ seconds...Zzz )\n");
					Thread.sleep(40000); // 30,000 = 30 seconds
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("________________________________");
		System.out.println(" Total " + count + " downloaded.");
		System.out.println(" Job Completed! Bye");
		System.exit(0);
	}

	/**
	 * 
	 * http://www.ebookshare.net/software/FireFox-Secrets-2005-RETAiL-eBOOk-
	 * rebOOk-12070.html ==>
	 * FireFox-Secrets-2005-RETAiL-eBOOk-rebOOk-12070.torrent
	 * 
	 * @param URLname
	 * @return
	 */
	public static String getTorrentFileNameFromURL(String URLname) {
		String extension = ".torrent";
		String[] urlTempSting = URLname.split("/");
		URLname = urlTempSting[urlTempSting.length - 1];
		String[] urlFile2 = URLname.split("\\.");
		URLname = urlFile2[0] + extension;
		return URLname;
	}
}
